package com.aps.server.controller;

import com.aps.common.entity.Result;
import com.aps.server.entity.ApsOrder;
import com.aps.server.entity.BasicEquip;
import com.aps.server.entity.BasicProduct;
import com.aps.server.service.BasicEquipService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备信息控制器
 * 提供设备信息相关的REST API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/basic-equips")
@RequiredArgsConstructor
@Tag(name = "设备信息管理", description = "设备信息相关的API接口")
public class BasicEquipController {
    @Resource
     BasicEquipService basicEquipService;

    @PostMapping("/create")
    @Operation(summary = "创建设备信息", description = "创建一个新的设备信息")
    public Result<String> createEquip(@RequestBody BasicEquip basicEquip) {
        try {
            boolean success = basicEquipService.createEquip(basicEquip);
            if (success) {
                return Result.success(basicEquip.getId().toString(), "设备信息创建成功");
            } else {
                return Result.error(400, "设备信息创建失败");
            }
        } catch (Exception e) {
            log.error("创建设备信息失败", e);
            return Result.error(500, "创建设备信息失败: " + e.getMessage());
        }
    }

    @PostMapping("/modify")
    @Operation(summary = "更新设备信息", description = "根据设备ID更新设备信息")
    public Result<String> updateEquip(@RequestBody BasicEquip basicEquip) {
        try {
            String id = basicEquip.getId();
            if(id == null) {
                return Result.error(400, "数据错误","未获取到数据id！");
            }
            basicEquip.setId(id);
            boolean success = basicEquipService.updateEquip(basicEquip);
            if (success) {
                return Result.success("设备信息更新成功");
            } else {
                return Result.error(400, "设备信息更新失败");
            }
        } catch (Exception e) {
            log.error("更新设备信息失败", e);
            return Result.error(500, "更新设备信息失败: " + e.getMessage());
        }
    }



    @GetMapping("/del/{id}")
    @Operation(summary = "删除设备信息", description = "根据设备ID删除产品信息（使用配置的租户ID）")
    public Result<String> deleteEquip(
            @Parameter(description = "设备ID") @PathVariable("id") String id) {
        try {
            boolean success = basicEquipService.deleteEquip(id);
            if (success) {
                return Result.success("设备信息删除成功");
            } else {
                return Result.error(400, "设备信息删除失败");
            }
        } catch (Exception e) {
            log.error("删除设备信息失败", e);
            return Result.error(500, "删除设备信息失败: " + e.getMessage());
        }
    }

    /**
     * 查询当前租户的产品列表
     */
    @GetMapping("/current-tenant")
    @Operation(summary = "查询当前租户的设备列表", description = "根据配置的租户ID查询所有设备")
    public Result<List<BasicEquip>> getProductsByCurrentTenant(
            @RequestParam(value = "equipCode", defaultValue = "", required = false) String equipCode,
            @RequestParam(value = "equipName", defaultValue = "", required = false) String equipName,
            @RequestParam(value = "equipType", defaultValue = "", required = false) String equipType
    ) {
        try {
            List<BasicEquip> equips= basicEquipService.getEquipsByTenantId(equipCode,equipName,equipType);
            return Result.success(equips);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据当前租户ID查询设备失败", e);
            return Result.error(500, "查询设备失败: " + e.getMessage());
        }
    }

    /**
     * 查询订单对应使用的设备
     * @param orders
     * @return
     */
    @PostMapping("/getEquiptsByOrders")
    @Operation(summary = "根据订单获取依赖的设备")
    public Result<List<BasicEquip>> getBasicEquipsByTenantIdAndOrders(@RequestBody List<ApsOrder> orders){
        try {
            List<BasicEquip> basicEquipsByTenantIdAndOrders = basicEquipService.getBasicEquipsByTenantIdAndOrders(orders);
            return Result.success(basicEquipsByTenantIdAndOrders);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据订单获取依赖的设备失败", e);
            return Result.error(500, "查询设备失败: " + e.getMessage());
        }
    }
}
